import Taro from '@tarojs/taro'
import Pool from '../components/base/pool'

let instance
class store {
  frame = 0
  score = 0
  odlValue = Taro.getStorageSync('nm-page-score') || 0
  /**
   * 挂在屏幕上的金币
   */
  golds = []
  /**
   * 接失败的金币
   */
  fallDowns = []
  /**
   * 接住的金币
   */
  haveGolds = []
  /**
   * 运动中的金币
   */
  moveGolds = []
  /**
   * 加分数组
   */
  bonus = []
  enemys = []
  animations = []
  gameOver = false
  constructor() {
    if (instance) return instance

    instance = this

    this.pool = new Pool()

    this.reset()
  }

  reset() {
    this.frame = 0
    this.score = 0
    /**
     * 挂在屏幕上的金币
     */
    this.golds = []
    /**
     * 接失败的金币
     */
    this.fallDowns = []
    /**
     * 接住的金币
     */
    this.haveGolds = []
    /**
     * 加分数组
     */
    this.bonus = []
    this.enemys = []
    this.animations = []
    this.gameOver = false
    this.moveGolds = []
  }
  $updateScore(value) {
    this.score = value
    const odlValue = Taro.getStorageSync('nm-page-score') || 0
    if (odlValue < value) {
      Taro.setStorageSync('nm-page-score', value)
      this.odlValue = value
    }
  }
}

/**
 * 全局状态管理器
 */
export default new store()